package constant.milk.cashbook.database;

import java.util.Calendar;
import java.util.HashMap;

import constant.milk.cashbook.bean.BankData;
import constant.milk.cashbook.bean.CategoryMainData;
import constant.milk.cashbook.util.MilkUtil;
import android.content.Context;

public class DbSetting
{
	private Context context;
	private DbAdapter db;
	private DbInsertQuery insertQuery;
	private DBSelectQuery selectQuery;
	
	public DbSetting(Context context, DbAdapter db)
	{
		this.context = context;
		this.db = db;
		insertQuery = new DbInsertQuery(context, db);
		selectQuery = new DBSelectQuery(context, db);
	}
	
	public void setBeginTansaction() throws Exception
	{
		db.beginTansaction(context);
	}
	
	public void setSuccessful() throws Exception
	{
		db.setTransactionSuccessful();
	}
	
	public void setEndTansaction() throws Exception
	{
		db.endTransaction(context);
	}
	
	public void setSetting()
	{
		setCategoryMain();
		setCategorySub();
		setAsset();
		setBudget();
		setMain();
		setWidget();
	}
	
	private void setWidget()
	{
		insertWidget();
	}

	private void insertWidget()
	{
		// type 0 = 지출, 1 = 수입, 2 = 이동, 4 = 잔액 
		// asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체
		try
		{
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("type", "0");
			map.put("asset", "6");
			map.put("name", "");
			insertQuery.setWidgetSetting(map);
			
			map.clear();
			map.put("type", "1");
			map.put("asset", "6");
			map.put("name", "");
			insertQuery.setWidgetSetting(map);
			
			map.clear();
			map.put("type", "2");
			map.put("asset", "6");
			map.put("name", "");
			insertQuery.setWidgetSetting(map);
			
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void setMain()
	{
		insertMain();
	}

	private void insertMain()
	{
		// type 0 = 지출, 1 = 수입, 2 = 이동, 4 = 잔액 
		// asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체
		try
		{
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("type", "4");
			map.put("asset", "0");
			map.put("name", "");
			insertQuery.setMainSetting(map);
			
			map.clear();
			map.put("type", "4");
			map.put("asset", "1");
			map.put("name", "");
			insertQuery.setMainSetting(map);
			
			map.clear();
			map.put("type", "0");
			map.put("asset", "6");
			map.put("name", "");
			insertQuery.setMainSetting(map);
			
			map.clear();
			map.put("type", "1");
			map.put("asset", "6");
			map.put("name", "");
			insertQuery.setMainSetting(map);
			
			map.clear();
			map.put("type", "2");
			map.put("asset", "6");
			map.put("name", "");
			insertQuery.setMainSetting(map);
			
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void setBudget()
	{
		insertBudget();
	}
	
	private void insertBudget()
	{
		try
		{
			// 지출, 주간
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("type", "0");
			map.put("kind", "0");
			insertQuery.setBudget(map);
			// 지출, 월간
			map.clear();
			map.put("type", "0");
			map.put("kind", "1");
			insertQuery.setBudget(map);
			// 지출, 연간
			map.clear();
			map.put("type", "0");
			map.put("kind", "2");
			insertQuery.setBudget(map);
			
			// 수입, 주간
			map.clear();
			map.put("type", "1");
			map.put("kind", "0");
			insertQuery.setBudget(map);
			// 수입, 월간
			map.clear();
			map.put("type", "1");
			map.put("kind", "1");
			insertQuery.setBudget(map);
			// 수입, 연간
			map.clear();
			map.put("type", "1");
			map.put("kind", "2");
			insertQuery.setBudget(map);
			
			// 이동, 주간
			map.clear();
			map.put("type", "2");
			map.put("kind", "0");
			insertQuery.setBudget(map);
			// 이동, 월간
			map.clear();
			map.put("type", "2");
			map.put("kind", "1");
			insertQuery.setBudget(map);
			// 이동, 연간
			map.clear();
			map.put("type", "2");
			map.put("kind", "2");
			insertQuery.setBudget(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void setAsset()
	{
		insertRealMoney();
		insertBankBook();
		insertCheckCard();
		insertCreditCard();
		insertPhone();
		insertGfitCard();
	}
	
	private void insertRealMoney()
	{
		try
		{
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("addflag", "0");
			map.put("money", "0");
			map.put("name", "현금");
			map.put("memo", "초기 셋팅 자산");
			
			insertQuery.setRealMoneyInsert(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void insertBankBook()
	{
		try
		{
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("addflag", "0");
			map.put("money", "0");
			map.put("name", "은행");
			map.put("memo", "초기 셋팅 자산");
			
			insertQuery.setBankBookInsert(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void insertCheckCard()
	{
		try
		{
			BankData data = selectQuery.getBankName("은행");
			
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("addflag", "0");
			map.put("name", "체크카드");
			map.put("linkid", data.getId() + "");
			map.put("memo", "초기 셋팅 자산");
			
			insertQuery.setCheckCardInsert(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void insertCreditCard()
	{
		try
		{
			BankData data = selectQuery.getBankName("은행");
			
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("addflag", "0");
			map.put("money", "0");
			map.put("name", "신용카드");
			map.put("linkid", data.getId() + "");
			map.put("autoyn", "0");
			map.put("termdate", "1");
			map.put("resultdate", "5");
			
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH);
			int day = cal.get(Calendar.DAY_OF_MONTH);
			
			map.put("signdate", MilkUtil.getDateDB(year, month+1, day));
			map.put("memo", "초기 셋팅 자산");
			
			insertQuery.setCreditCardInsert(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void insertPhone()
	{
		try
		{
			BankData data = selectQuery.getBankName("은행");
			
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("addflag", "0");
			map.put("money", "0");
			map.put("name", "핸드폰");
			map.put("linkid", data.getId() + "");
			map.put("autoyn", "0");
			map.put("termdate", "1");
			map.put("resultdate", "5");
			
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH);
			int day = cal.get(Calendar.DAY_OF_MONTH);
			
			map.put("signdate", MilkUtil.getDateDB(year, month+1, day));
			map.put("memo", "초기 셋팅 자산");
			
			insertQuery.setPhoneInsert(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void insertGfitCard()
	{
		try
		{
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("count", "0");
			map.put("addflag", "0");
			map.put("money", "5000");
			map.put("name", "상품권");
			map.put("giftnumber", "0");
			map.put("memo", "초기 셋팅 자산");
			
			insertQuery.setGiftCardInsert(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void setCategoryMain()
	{
		insertCategoryMain("식비");
		insertCategoryMain("교통");
		insertCategoryMain("세금");
		insertCategoryMain("급여");
		insertCategoryMain("저축");
	}
	
	private void insertCategoryMain(String category)
	{
		try
		{
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("name", category);
			insertQuery.setCategoryMain(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void setCategorySub()
	{
		insertCategorySub("식비", "아침");
		insertCategorySub("식비", "점심");
		insertCategorySub("식비", "저녁");
		insertCategorySub("교통", "지하철");
		insertCategorySub("교통", "버스");
		insertCategorySub("교통", "택시");
		insertCategorySub("세금", "전기세");
		insertCategorySub("세금", "수도세");
		insertCategorySub("세금", "가스요금");
		insertCategorySub("급여", "월급");
		insertCategorySub("급여", "보너스");
		insertCategorySub("저축", "정기적금");
		insertCategorySub("저축", "주택청약");
		insertCategorySub("저축", "교육적금");
	}
	
	private void insertCategorySub(String categoryMain, String category)
	{
		DBSelectQuery select = new DBSelectQuery(context, db);
		
		CategoryMainData data = select.getCategoryMainName(categoryMain);
		
		try
		{
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("name", category);
			map.put("linkid", data.getId());
			
			insertQuery.setCategorySub(map);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void setResetDb() throws Exception
	{
		db.createTable(context);
		
		db.delete(context, TableName.REAL_MONEY, null, null);
		db.delete(context, TableName.BANK_BOOK, null, null);
		db.delete(context, TableName.CHECK_CARD, null, null);
		db.delete(context, TableName.CREDIT_CARD, null, null);
		db.delete(context, TableName.PHONE, null, null);
		db.delete(context, TableName.GIFT_CARD, null, null);
		db.delete(context, TableName.IN_OUT, null, null);
		db.delete(context, TableName.CATEGORY_MAIN, null, null);
		db.delete(context, TableName.CATEGORY_SUB, null, null);
		db.delete(context, TableName.AUTO_MAIN, null, null);
		db.delete(context, TableName.SMS, null, null);
		db.delete(context, TableName.HALBU, null, null);
		db.delete(context, TableName.BUDGET, null, null);
		db.delete(context, TableName.MAIN_SETTING, null, null);
		db.delete(context, TableName.WIDGET_SETTING, null, null);
		
		setSetting();
	}
}
